const request = (options) => {
  const token = uni.getStorageSync('token')
  
  return new Promise((resolve, reject) => {
    uni.request({
      ...options,
      header: {
        ...options.header,
        'Authorization': token ? `Bearer ${token}` : ''
      },
      success: (res) => {
        if (res.data.code === 401) {
          // Token过期或未登录
          uni.navigateTo({
            url: '/pages/login/login'
          })
          reject(res)
        } else {
          resolve(res)
        }
      },
      fail: reject
    })
  })
}

export default request 